//
//  DetailViewController.m
//  RepasoApp
//
//  Created by Vicente Vicens Camarena on 02/10/11.
//  Copyright 2011 Imathink World, S.L.N.E. All rights reserved.
//

#import "DetailViewController.h"

#import "RootViewController.h"
#import "Resource.h"

@interface DetailViewController ()
@property (nonatomic, retain) UIPopoverController *popoverController;
- (void)configureView;

@property (nonatomic, retain) WebViewController *websiteViewController;
@end

@implementation DetailViewController

@synthesize toolbar = _toolbar;
@synthesize detailItem = _detailItem;
@synthesize detailWebView = _detailWebView;
@synthesize popoverController = _myPopoverController;
@synthesize websiteViewController = websiteViewController_;

#pragma mark - Managing the detail item

/*
 When setting the detail item, update the view and dismiss the popover controller if it's showing.
 */
- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        [_detailItem release];
        _detailItem = [newDetailItem retain];
        
        // Update the view.
        [self configureView];
    }

    if (self.popoverController != nil) {
        [self.popoverController dismissPopoverAnimated:YES];
    }        
}

- (void)configureView
{
    Resource * resource = (Resource *)self.detailItem;

    NSLog(@"Path: %@", [resource localPath]);
    NSURL *url = [NSURL fileURLWithPath:[resource localPath]];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [self.detailWebView loadRequest:requestObj];
    [self.detailWebView setHidden: NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    self.detailWebView.hidden = YES;
    
    self.detailWebView.delegate = self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

#pragma mark - Split view support

- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController: (UIPopoverController *)pc
{
    barButtonItem.title = @"Events";
    NSMutableArray *items = [[self.toolbar items] mutableCopy];
    [items insertObject:barButtonItem atIndex:0];
    [self.toolbar setItems:items animated:YES];
    [items release];
    self.popoverController = pc;
}

// Called when the view is shown again in the split view, invalidating the button and popover controller.
- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    NSMutableArray *items = [[self.toolbar items] mutableCopy];
    [items removeObjectAtIndex:0];
    [self.toolbar setItems:items animated:YES];
    [items release];
    self.popoverController = nil;
}

/*
 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
 */

- (void)viewDidUnload
{
    [self setDetailWebView:nil];
	[super viewDidUnload];

	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	self.popoverController = nil;
}

#pragma mark - Memory management

- (void)didReceiveMemoryWarning
{
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [websiteViewController_ release];
    [_myPopoverController release];
    [_toolbar release];
    [_detailItem release];
    [_detailWebView release];
    [super dealloc];
}

- (void)webViewControllerWantsToClose:(WebViewController *)webViewController {
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)showWebsite:(id)sender {
    if (!self.websiteViewController) {
        self.websiteViewController = [[[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
        self.websiteViewController.vvDelegate = self;
        self.websiteViewController.modalPresentationStyle = UIModalPresentationFullScreen;
        self.websiteViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    }
    [self presentModalViewController:self.websiteViewController animated:YES];
}
@end
